package com.homework.ch13;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/10/21
 * @Description:
 */
public class Test {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        //获得字节码对象：类.class 对象.getclass() Class.forName("全类名")
        Class<?> clazz = Teacher.class;
        Constructor<?> constructor = clazz.getConstructor();
        Teacher t1= (Teacher) constructor.newInstance();
        Field field = clazz.getDeclaredField("name");
        field.setAccessible(true);
        field.set(t1, "tom");
        System.out.println(field.get(t1));

        Constructor<?> constructor1 = clazz.getDeclaredConstructor(String.class,int.class,double.class);
        constructor1.setAccessible(true);
        Teacher t2= (Teacher) constructor1.newInstance("zs",25,35.5);
        Method method=clazz.getMethod("getAge");
        System.out.println(method.invoke(t2));
        Method method1=clazz.getDeclaredMethod("setSalary",double.class);
        method1.setAccessible(true);
        method1.invoke(t2,7890.5);
        System.out.println(t2);
    }
}
